package com.jsmtr.rms.controller;

import com.jsmtr.rms.entity.*;
import com.jsmtr.rms.service.*;
import com.jsmtr.rms.utils.DateTools;
import com.jsmtr.rms.utils.EnumTools;
import com.jsmtr.rms.utils.StringTools;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;


@Controller
@RequestMapping("/Do/SendInfos")
public class DoSendInfosController {

    @Resource
    private IUtilService utilService;

    @Resource
    private IDoGXService doGXService;

    @Resource
    private IRecommendService recommendService;

    @Resource
    private ISchoolService schoolService;

    @Resource
    private IDoZZService doZZService;

    @Resource
    private IDoSZService szService;
    @Resource
    private IDoBSService bsService;

    @RequestMapping(value = "/SendInfosAction", method = RequestMethod.POST)
    public void SendInfosAction(HttpServletRequest request, HttpServletResponse response) {
        try {
            String pid = request.getParameter("pid").trim();//招聘方案ID
            String fid = request.getParameter("fid").trim();//招聘流程ID
            String deptcode = request.getParameter("deptcode").trim();//责任部门编码
            String infoContent = request.getParameter("infoContent").trim();//信息内容

            String[] ids = request.getParameterValues("id");//人员信息ID
            String[] cardnos = request.getParameterValues("cardno");//人员信息ID
            if (ids != null) {
                ProjectInfo projectInfo = new ProjectInfo();
                ProjectInfoFlow projectInfoFlow = new ProjectInfoFlow();
                boolean temp = false;
                if(pid.indexOf("A_")>=0){//社招
                    projectInfo = doGXService.getProject(pid);
                    projectInfoFlow = doGXService.getCurrentFlow(pid, deptcode, fid);
                    temp = true;
                }else if((pid.indexOf("B_")>=0)||((pid.indexOf("F_")>=0))){//高校
                    projectInfo = szService.getProject(pid);
                    projectInfoFlow = szService.getCurrentFlow(pid, deptcode, fid);
                    temp = true;
                }if((pid.indexOf("C_")>=0)||((pid.indexOf("E_")>=0))){//中专
                    projectInfo = doZZService.getProject(pid);
                    projectInfoFlow = doZZService.getCurrentFlow(pid, deptcode, fid);
                    temp = true;
                }if(pid.indexOf("D_")>=0){//内部
                    projectInfo = doZZService.getProject(pid);
                    projectInfoFlow = doZZService.getCurrentFlow(pid, deptcode, fid);
                    temp = true;
                }

                if(temp){
                    List<SendInfos> editLists = new ArrayList<SendInfos>();
                    for (int i = 0; i < ids.length; i++) {
                        SendInfos sendInfos = new SendInfos();
                        sendInfos.setId(UUID.randomUUID().toString());
                        sendInfos.setUserid(ids[i]);
                        sendInfos.setCardno(cardnos[i]);
                        sendInfos.setPid(pid);
                        sendInfos.setFid(projectInfoFlow.getFid());
                        sendInfos.setNickname(projectInfoFlow.getNickname());
                        sendInfos.setInfos(infoContent);
                        sendInfos.setIsread(0);
                        sendInfos.setSendtime(new Date());
                        sendInfos.setSenduid(projectInfo.getAdduid());
                        sendInfos.setSenduname(projectInfo.getAdduname());

                        editLists.add(sendInfos);
                    }
                    if (editLists.size() > 0) {
                        utilService.addList(editLists);
                        utilService.printJsonResult(true, "操作成功！", "no", response);
                    } else
                        utilService.printJsonResult(false, "当前选中的信息不能执行此操作！", "no", response);
                }else
                    utilService.printJsonResult(false, "当前选中的信息不能执行此操作！", "no", response);
            } else {
                utilService.printJsonResult(false, "请选择应聘者！", "no", response);
            }
        } catch (Exception e) {
            e.printStackTrace();
            utilService.printJsonResult(false, "操作失败！", "no", response);
        }
    }

}